package com.ma4567.remotecontroller.FileRequest;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.ma4567.remotecontroller.MysqlHelper.TableLogManager;
import com.ma4567.remotecontroller.MysqlHelper.TableRequestLog;
import com.ma4567.remotecontroller.RemoteControllerApplication;
import com.ma4567.remotecontroller.ffmpegManager.ffmpegController;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;

class getPreviousPathResponse extends com.ma4567.remotecontroller.util.CustomRespone{
    public String PreviousPath;
    public getPreviousPathResponse(int Status,String PreviousPath) {
        super(Status);
        this.PreviousPath = PreviousPath.replace("\\", "%5C");
    }
}

@CrossOrigin
@RestController
public class GetPreviousLayer {
    @RequestMapping(value = "/api/getPreviousPath", method = RequestMethod.GET)
    public String getCurrentPath(HttpServletRequest request,
                                 @RequestParam("cookie") String Cookie) throws SQLException {
        ffmpegController.StopStream();
        TableRequestLog.AddLog("Request previous path", Cookie, request.getRemoteAddr());
        if(!RemoteControllerApplication.userManager.JudgeCookie(Cookie)){//鉴权
            return JSONObject.toJSONString(new getPreviousPathResponse(1, "null"), SerializerFeature.PrettyFormat);
        }
        else {
            TableLogManager.AddLog("Request previous path", Cookie);
            return JSONObject.toJSONString(new getPreviousPathResponse(0, RemoteControllerApplication.userManager.GetUserPreviousPath(Cookie)), SerializerFeature.PrettyFormat);
        }
    }
}
